package cn.xuchunh.campusbbs.storage.sp;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import org.w3c.dom.Text;

import cn.xuchunh.campusbbs.AppContext;
import cn.xuchunh.campusbbs.utils.Constants;

/**
 * Created by XuChunH on 2015/8/31.
 */
public class SharedPreferenceManager {

    private static final String NAME = "campus_bbs";

    public static final String DEFAULT_FORUM_KEY = "CBQJazn9Wws8Ivhr6U";
    public static final String DEFAULT_APP_NAME = "清水河畔";
    public static final String DEFAUL_SDK_VERSION = "2.4.0";
    public static final String DEFAULT_PACKAGE_NAME = "com.appbyme.app118563";
    public static final String DEFAULT_SERVER_DOMAIN = "bbs.uestc.edu.cn";

    private static SharedPreferenceManager instance;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    private static final String USER_NAME = "username";
    private static final String PASSWORD = "password";
    private static final String AUTOLOGIN = "autologin";
    private static final String FORUM_KEY = "forum_key";
    private static final String APP_NAME = "app_name";
    private static final String SDK_VERSION = "sdk_version";
    private static final String PACKAGE_NAME = "package_name";
    private static final String SERVER_DOMAIN = "server_domain";
    private static final String UI_MODE = "ui_mode";
    private static final String USER_AVATAR = "user_avatar";

    public synchronized static SharedPreferenceManager getInstance() {
        if (instance == null) {
            instance = new SharedPreferenceManager(AppContext.getInstance().getApplicationContext());
        }
        return instance;
    }

    private SharedPreferenceManager(Context context) {
        sharedPreferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
    }

    public void setUserName(String userName) {
        editor.putString(USER_NAME, userName);
        editor.commit();
    }

    public String getUsername() {
        return sharedPreferences.getString(USER_NAME, "");
    }

    public void setPassword(String password) {
        editor.putString(PASSWORD, password);
        editor.commit();
    }

    public String getPassword() {
        return sharedPreferences.getString(PASSWORD, "");
    }

    public void setUserAvatar(String avatar) {
        if (!TextUtils.isEmpty(avatar)) {
            editor.putString(USER_AVATAR, avatar);
            editor.commit();
        }
    }

    public String getUserAvatar() {
        return sharedPreferences.getString(USER_AVATAR, "");
    }

    public void setAutologin(boolean autologin) {
        editor.putBoolean(AUTOLOGIN, autologin);
        editor.commit();
    }

    public boolean isAutoLogin() {
        return sharedPreferences.getBoolean(AUTOLOGIN, false);
    }

    public void setForumKey(String forumKey) {
        editor.putString(FORUM_KEY, forumKey);
        editor.commit();
    }

    public String getForumKey() {
        return sharedPreferences.getString(FORUM_KEY, DEFAULT_FORUM_KEY);
    }

    public void setAppName(String appName) {
        editor.putString(APP_NAME, appName);
        editor.commit();
    }

    public String getAppName() {
        return sharedPreferences.getString(APP_NAME, DEFAULT_APP_NAME);
    }

    public void setSdkVersion(String sdkVersion) {
        editor.putString(SDK_VERSION, sdkVersion);
        editor.commit();
    }

    public String getSdkVersion() {
        return sharedPreferences.getString(SDK_VERSION, DEFAUL_SDK_VERSION);
    }

    public void setPackageName(String packageName) {
        editor.putString(PACKAGE_NAME, packageName);
        editor.commit();
    }

    public String getPackageName() {
        return sharedPreferences.getString(PACKAGE_NAME, DEFAULT_PACKAGE_NAME);
    }

    public void setServerDomain(String serverDomain) {
        editor.putString(SERVER_DOMAIN, serverDomain);
        editor.commit();
    }

    public String getServerDomain() {
        return sharedPreferences.getString(SERVER_DOMAIN, DEFAULT_SERVER_DOMAIN);
    }

    public void setUiMode(int mode) {
        editor.putInt(UI_MODE, mode);
        editor.commit();
    }

    public int getUiMode() {
        return sharedPreferences.getInt(UI_MODE, Constants.UI_MODE_BOTTOM);
    }

}
